package shiyan3;

import java.io.IOException;
import java.net.*;

public class UDPClient {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket();
        String data = "我是Client";
        byte[] sendbuf =data.getBytes();
        //DatagramPacket sendpacket = new DatagramPacket(sendbuf, sendbuf.length, InetAddress.getLocalHost(),20000);
        DatagramPacket sendpacket = new DatagramPacket(
                sendbuf,
                sendbuf.length,
                InetAddress.getByName("192.168.1.100"),  // 替换为目标IP地址
                20000  // 目标端口号
        );
        socket.send(sendpacket);

        byte[] receivebuf = new byte[1024];
        DatagramPacket receivePacket = new DatagramPacket(receivebuf, receivebuf.length);
        socket.receive(receivePacket);
        String ip = receivePacket.getAddress().getHostAddress();
        int port = receivePacket.getPort();
        String data2 = new String(receivePacket.getData(), 0, receivePacket.getLength());
        System.out.println(ip + ":" + port);
        System.out.println(data2);

        socket.close();
    }
}
